﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FrbaBus.Core;

namespace FrbaBus.Compra_de_Pasajes
{
    public partial class CompraPasajes : Form
    {
        public CompraPasajes()
        {
            InitializeComponent();
        }

        public bool origen = false;
        public DateTime dia;
        public string diaHora;
        public static double pago;
        public DataGridViewRow Row;
        

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            
            DBManager db = new DBManager();

            if (checkBox1.Checked == true)
            {
                SqlCommand consulta11 = new SqlCommand("exec ALL_IN.ViajesDisponibles @Fecha_Salida = '" + diaHora + "'"
                  + ", @Origen = '" + textBox3.Text + "'" + ", @Destino = '" + textBox4.Text + "'");
                SQLResponse r11 = db.executeQuery(consulta11);

                SqlCommand consulta12 = new SqlCommand("select @@rowcount");
                SQLResponse r12 = db.executeQuery(consulta12);
                int valor2 = (int)r12.result.Rows[0][0];
                
                for (int i = 0; i < valor2; i++)
                {

                    DateTime hola = Convert.ToDateTime(r11.result.Rows[i][1].ToString());

                    dataGridView2.Rows.Add(hola.ToString("yyyy-MM-dd HH:ss:mm"), r11.result.Rows[i][8].ToString()
                        , r11.result.Rows[i][7].ToString(), r11.result.Rows[i][9].ToString()
                        , r11.result.Rows[i][4].ToString(), r11.result.Rows[i][10].ToString(),
                        r11.result.Rows[i][11].ToString(),
                    r11.result.Rows[i][6].ToString(),
                    r11.result.Rows[i][5].ToString());
                }


            }
            else
            {
                SqlCommand consulta11 = new SqlCommand("exec ALL_IN.ViajesDisponibles2 @Origen = '" + textBox3.Text + "'" + ", @Destino = '" + textBox4.Text + "'");
                SQLResponse r11 = db.executeQuery(consulta11);

                SqlCommand consulta12 = new SqlCommand("select @@rowcount");
                SQLResponse r12 = db.executeQuery(consulta12);
                int valor2 = (int)r12.result.Rows[0][0];
                

                
                for (int i = 0; i < valor2; i++)
                {
                    DateTime hola = Convert.ToDateTime(r11.result.Rows[i][1].ToString());

                    dataGridView2.Rows.Add(hola.ToString("yyyy-MM-dd HH:ss:mm"), r11.result.Rows[i][8].ToString()
                        , r11.result.Rows[i][7].ToString(), r11.result.Rows[i][9].ToString()
                        , r11.result.Rows[i][4].ToString(), r11.result.Rows[i][10].ToString(),
                        r11.result.Rows[i][11].ToString(),
                    r11.result.Rows[i][6].ToString(),
                    r11.result.Rows[i][5].ToString());
                }


            }


        }

        private void button2_Click(object sender, EventArgs e)
        {

            origen = true;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
        
        }

        private void button3_Click(object sender, EventArgs e)
        {
            origen = false;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
            
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dia = dateTimePicker1.Value;
            diaHora = dia.ToString("yyyy-MM-dd");
            diaHora = diaHora + " 00:00:00.000";
            //MessageBox.Show(diaHora);
            //"yyyy-MM-dd hh:mm:ss.sss"
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void CompraPasajes_Load(object sender, EventArgs e)
        {
            dateTimePicker1.MinDate = Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["Fecha"]);
            dia = dateTimePicker1.Value;
            diaHora = dia.ToString("yyyy-MM-dd");
            diaHora = diaHora + " 00:00:00.000";
        }

        private void button5_Click(object sender, EventArgs e)
        {
                
                if (dataGridView2.SelectedRows.Count != 0)
                {

                    Row = dataGridView2.SelectedRows[0];
                    Compra_de_Pasajes.SeleccionarEncomiendas frm = new Compra_de_Pasajes.SeleccionarEncomiendas(this);
                    frm.ShowDialog(this);

                    //this.Close();
                }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count != 0)
            {

                Row = dataGridView2.SelectedRows[0];
                Compra_de_Pasajes.SeleccionarButacas frm = new Compra_de_Pasajes.SeleccionarButacas(this);
                frm.ShowDialog(this);

               //this.Close();
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {


            if (dataGridView1.Rows.Count <= 0 && dataGridView3.Rows.Count <= 0)
            {
                MessageBox.Show("Debe seleccionar butacas/encomiendas para realizar una compra.");
            }
            else
            {
                
                string[] words = dataGridView2.SelectedRows[0].Cells[0].Value.ToString().Split('-');
                string[] words2 = words[2].Split(' ');
                string dia1 = words[0];
                string mes1 = words[1];
                string anio1 = words2[0];
                
                int result = 0;
                int i = 0;
                int disc = 0;
                for (i=0; i <= dataGridView1.Rows.Count - 1; i++)
                {
                    DBManager db = new DBManager();
                    SqlCommand consulta2 = new SqlCommand("select ALL_IN.clienteenviaje ('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView2.SelectedRows[0].Cells[3].Value.ToString() + "','" + anio1 + "','" + mes1 + "','" + dia1 + "')");
                    SQLResponse r = db.executeQuery(consulta2);
                    if (r.result.Rows[0][0].ToString() == "True")
                    {
                        result = 1;
                        MessageBox.Show("El Cliente con DNI: " + dataGridView1.Rows[i].Cells[0].Value.ToString() + " ya se encuentra realizando un viaje en esa fecha.");
                    }
                    if (dataGridView1.Rows[i].Cells[6].Value.ToString() == "1")
                    {
                        disc = disc + 1;
                    }



                }
                if (disc > 1)
                {
                    MessageBox.Show("No puede haber mas de dos personas discapacitadas para la misma compra.");
                }
                if (result == 0 && disc <=1 )
                {
                    if (dataGridView1.Rows.Count > 0)
                    {
                        int k = 0;
                        for (k = 0; k <= dataGridView1.Rows.Count - 1; k++)
                        {
                           
                            double precio = Convert.ToDouble(dataGridView1.Rows[k].Cells[4].Value.ToString());
                            pago = pago + precio;

                        }
                       
                    }
                    if (dataGridView3.Rows.Count > 0)
                    {
                        int k = 0;
                        for (k = 0; k <= dataGridView3.Rows.Count - 1; k++)
                        {

                            double precio = Convert.ToDouble(dataGridView3.Rows[k].Cells[1].Value.ToString());
                            pago = pago + precio;

                        }

                    }

                    Compra_de_Pasajes.RealizarPago frm = new Compra_de_Pasajes.RealizarPago(this);
                    frm.ShowDialog(this);
                }
                else
                {
                    MessageBox.Show("Por favor, verifique los errores.");
                }

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dataGridView3.Rows.Clear();
        }

        private void selection(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            dataGridView3.Rows.Clear();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                dateTimePicker1.Enabled = true;

            }
            else
            {
                dateTimePicker1.Enabled = false;
            }
        }
    }
}

